/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.inventory.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.inventory.entity.HostInventory;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtCabinet;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtHostsAccessory;
import com.thinkgem.jeesite.modules.inventory.service.HostInventoryService;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtCabinetService;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtHostsAccessoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 资产管理Controller
 * @author johnKee
 * @version 2016-07-20
 */
@Controller
@RequestMapping(value = "${adminPath}/inventory/zlmtCabinet")
public class ZlmtCabinetController extends BaseController {

	@Autowired
	private ZlmtCabinetService zlmtCabinetService;

	@Autowired
	private HostInventoryService hostInventoryService;

	@Autowired
	private ZlmtHostsAccessoryService zlmtHostsAccessoryService;
	
	@ModelAttribute
	public ZlmtCabinet get(@RequestParam(required=false) String id) {
		ZlmtCabinet entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zlmtCabinetService.get(id);
		}
		if (entity == null){
			entity = new ZlmtCabinet();
		}
		return entity;
	}

	@RequestMapping(value = {"list", ""})
	public String list(ZlmtCabinet zlmtCabinet, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ZlmtCabinet> page = zlmtCabinetService.findPage(new Page<ZlmtCabinet>(request, response), zlmtCabinet); 
		model.addAttribute("page", page);
		return "modules/inventory/zlmtCabinetList";
	}


	@RequestMapping(value = "form")
	public String form(ZlmtCabinet zlmtCabinet, Model model) {
		model.addAttribute("zlmtCabinet", zlmtCabinet);
		return "modules/inventory/zlmtCabinetForm";
	}


	@RequestMapping(value = "save")
	public String save(ZlmtCabinet zlmtCabinet, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zlmtCabinet)){
			return form(zlmtCabinet, model);
		}
		zlmtCabinetService.save(zlmtCabinet);
		addMessage(redirectAttributes, "保存资产信息成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/zlmtCabinet/?repage";
	}
	

	@RequestMapping(value = "delete")
	public String delete(ZlmtCabinet zlmtCabinet, RedirectAttributes redirectAttributes) {
		zlmtCabinetService.delete(zlmtCabinet);
		addMessage(redirectAttributes, "删除资产信息成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/zlmtCabinet/?repage";
	}


	/**
	 * 获取资产JSON数据。
	 * @param extId 排除的ID
	 * @param type	类型（1：机柜；2：主机：3：配件；）
	 * @param grade 显示级别
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, @RequestParam(required = false) String type,
											  @RequestParam(required = false) Long grade, @RequestParam(required = false) Boolean isAll, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Map<String, Object> rootmap = Maps.newHashMap();
		rootmap.put("id", "-1");
		rootmap.put("pId", "0");
		rootmap.put("pIds", "0,");
		rootmap.put("name", "整个机房");
        rootmap.put("isParent", true);
		mapList.add(rootmap);

		Map<String, String> parentsMap = Maps.newHashMap();
		parentsMap.put("-1", "0,");
		List<ZlmtCabinet> list = zlmtCabinetService.findList(new ZlmtCabinet());
		List<HostInventory> hostInventoryList = null;
		List<ZlmtHostsAccessory> accessoryList = null;
		if(type.equals("2") || type.equals("3")){
			hostInventoryList = hostInventoryService.findListWithLoc(new HostInventory());
			if(type.equals("3")){
				accessoryList = zlmtHostsAccessoryService.findList(new ZlmtHostsAccessory());
			}
		}
		for (int i = 0; i < list.size(); i++) {
			ZlmtCabinet e = list.get(i);
			if (!parentsMap.containsKey(String.valueOf(e.getRow()))) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getRow());
				map.put("pId", "-1");
				map.put("pIds", "0,-1,");
				map.put("name", e.getRow() + "排机柜");
                map.put("isParent", true);
				parentsMap.put(String.valueOf(e.getRow()), "0,-1,");
				mapList.add(map);
			}
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getRow());
			map.put("pIds", parentsMap.get(String.valueOf(e.getRow())) + e.getRow() + ",");
			map.put("name", "编号" + e.getCabinetCode() + "第" + e.getColumns() + "列机柜");
            if(!type.equals("1")){
                map.put("isParent", true);
            }
			parentsMap.put(e.getId(), map.get("pIds").toString());
			mapList.add(map);
			if(type.equals("2") || type.equals("3")){
				for(int j = 0; j< hostInventoryList.size(); j++){
					HostInventory hostInventory = hostInventoryList.get(j);
                    if(hostInventory.getCabinetId().equals(e.getId())) {
                        Map<String, Object> hostMap = Maps.newHashMap();
                        hostMap.put("id", hostInventory.getId());
                        hostMap.put("pId", hostInventory.getCabinetId());
                        hostMap.put("pIds", parentsMap.get(hostInventory.getCabinetId()) + hostInventory.getCabinetId() + ",");
                        hostMap.put("name", "监控名" + hostInventory.getName() + "别名" + hostInventory.getAlias());
                        if (type.equals("3")) {
                            hostMap.put("isParent", true);
                        }
                        parentsMap.put(hostInventory.getId(), map.get("pIds").toString());
                        mapList.add(hostMap);
                        if(type.equals("3")){
                            for(int k=0; k<accessoryList.size(); k++){
                                ZlmtHostsAccessory accessory = accessoryList.get(k);
                                if(accessory.getHostid().equals(hostInventory.getId())) {
                                    Map<String, Object> accessoryMap = Maps.newHashMap();
                                    accessoryMap.put("id", accessory.getId());
                                    accessoryMap.put("pId", accessory.getHostid());
                                    accessoryMap.put("pIds", parentsMap.get(accessory.getHostid()) + accessory.getHostid() + ",");
                                    accessoryMap.put("name", accessory.getAccessoryName());
                                    mapList.add(accessoryMap);
                                }
                            }
                        }
                    }
				}

			}
		}
		return mapList;
	}

}